package com.ysy.tz.core.selfcomp;

import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;

public class DateTableColumn extends TableColumn {
    private StringProperty pattern = new SimpleStringProperty("yyyy-MM-dd");
    SimpleDateFormat format = new SimpleDateFormat();

    public DateTableColumn() {
        this.format.applyPattern((String)this.pattern.get());
        this.pattern.addListener((observableValue, oldValue, newValue) -> {
            this.format.applyPattern((String)this.pattern.get());
        });
        this.setCellFactory((column) -> {
            TableCell<Object, Date> cell = new TableCell<Object, Date>() {
                protected void updateItem(Date item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        this.setText((String)null);
                    } else {
                        this.setText(DateTableColumn.this.format.format(item));
                    }

                }
            };
            return cell;
        });
    }

    public String getPattern() {
        return (String)this.pattern.get();
    }

    public StringProperty patternProperty() {
        return this.pattern;
    }

    public void setPattern(String pattern) {
        this.pattern.set(pattern);
    }
}
